#!/usr/bin/python
# -*- coding: utf-8 -*-
import maya.cmds as mc
import maya.mel as mel
import sys

import OF3D_PublicClass.OF3D_zchPipePublicClass as ikspline
reload( ikspline )
ikSpl = ikspline.PublicClass()
#"2012/08/12"
__version__ = "2012/12/07"

def char_stretchSplineIk(splineName,jointNum,scTr,keepVolume,splineType=2):
        print ikSpl.ikSplineFromCurve(splineName,jointNum,scTr,keepVolume,splineType)
        
def char_stretchSplineIkUI():
        if(mc.window('char_createSplineIkWin',ex=True)):
                mc.deleteUI('char_createSplineIkWin')
        imp = "import OF3D_PublicClass.OF3D_zchPipePublicClass as ikspline\nikSpl = ikspline.PublicClass()\n"
                
        mc.window('char_createSplineIkWin',t="Stretchy IkSpline v%s"%__version__,menuBar=True)
        ikSpl.winMenuBar("pass","buildStretchyIkspline.mht")
        tabs = mc.tabLayout("cs_tableLayout_tl",innerMarginWidth=0,innerMarginHeight=0)
        
        byCurve_ = mc.frameLayout(l='#OF-SplineIkCreate' ,collapsable= 0 ,borderStyle ="etchedOut" ,labelVisible =0 ,marginWidth =0 ,marginHeight =0)
        mc.columnLayout(adj=1)
        nameButton = mc.textFieldGrp('OF_zchSplineIkName_tfg',l='SplineName: ',text='L_',columnWidth2=[85,200],cl2=['center','left'])
        #zchSplineIkType = mc.radioButtonGrp('zchSplineIkType',label='  SplineIk Type  : ',numberOfRadioButtons=2,labelArray2=['Spine SplineIk','Offest SplineIk'],columnWidth3=[90,85,95],columnAlign3=['left','left','left'],sl=1)
        mc.separator(w=255,style='single')
        njnts = mc.intSliderGrp('OF_zchSplineIkNJnts_isg',label='Joint Num:',h=25,min=2,max=99,v=5,field=True,columnWidth3=[90,30,120],adjustableColumn3=3)
        mc.separator(w=255,style='single')
        mc.checkBoxGrp('OF_zchSplineIkOption_cbg',label="   ",ncb=2,la2=['Stretch','keepVolume'],columnWidth3=[30,120,120],va2=[1,1])
        mc.separator(w=255,style='single')
        zchSplineIkType = mc.radioButtonGrp('OF_zchSplineIkStrType_rbg',label='  Stretch: ',sl=2,
                                                        numberOfRadioButtons=2,
                                                        labelArray2=['Scale','Translate'],
                                                        columnWidth3=[60,90,90],
                                                        columnAlign3=['left','left','left'])
        mc.radioButtonGrp('OF_zchSplineIkKvType_rbg',l='  Effect:',numberOfRadioButtons=2,labelArray2=['Average','Subsection'],sl=2,columnWidth3=[60,90,90],columnAlign3=['left','left','left'])
        mc.separator(w=255,style='single')
        importStr = "import %s as ikSplCreate\nikSplCreate.OF_zchBuildSplineIk()"%( __name__ )
        applyButton = mc.button(w=255,l='------------Apply------------',c=importStr)
        mc.separator(w=255,style='single')
        Blog = mc.text('(Author) OF3D-TD| Zhao ChunHai 2012/08/10',en=0)
        mc.separator(w=255,style='single')
        mc.setParent('..')
        mc.setParent(tabs)
        
        byJoint_ = mc.frameLayout( collapsable= 0 ,borderStyle ="etchedOut" ,labelVisible =0 ,marginWidth =0 ,marginHeight =0)
        mc.columnLayout(adj=True)
        mc.textFieldGrp("ikspl_fjSetSplName_tfg",l='SplineName: ',text='R_',columnWidth2=[85,200],cl2=['center','left'])
        mc.separator(w=255,style='single')
        mc.textFieldButtonGrp("ikspl_fjLoadIkHandle_tfg",l='ikHandle: ',text='ikHandle1',h=25,columnWidth3=[85,200,50],cl2=['center','left'],bl="<<Load",bc="%sikSpl.loadSelectedIntoButtonGrp('ikspl_fjLoadIkHandle_tfg')"%(imp) )
        #mc.textFieldButtonGrp("ikspl_fjLoadStartJnt_tfg",l='StartJot: ',text='',columnWidth3=[85,200,50],cl2=['center','left'],bl="<<Load",bc="%sikSpl.loadSelectedIntoButtonGrp('ikspl_fjLoadStartJnt_tfg')"%(imp) )
        #mc.textFieldButtonGrp("ikspl_fjLoadEndJnt_tfg",l='EndJot:   ',text='',columnWidth3=[85,200,50],cl2=['center','left'],bl="<<Load",bc="%sikSpl.loadSelectedIntoButtonGrp('ikspl_fjLoadEndJnt_tfg')"%(imp) )
        
        mc.separator(w=255,style='single')
        mc.checkBoxGrp('ikspl_fjIkOption_cbg',label="   ",ncb=2,la2=['Stretch','keepVolume'],columnWidth3=[30,120,120],va2=[1,1])
        mc.separator(w=255,style='single')
        mc.radioButtonGrp('ikspl_fjStrType_rbg',label='  Stretch: ',sl=2,
                                                        numberOfRadioButtons=2,
                                                        labelArray2=['Scale','Translate'],
                                                        columnWidth3=[60,90,90],
                                                        columnAlign3=['left','left','left'])
        mc.radioButtonGrp('ikspl_fjIkKvType_rbg',l='  Effect:',numberOfRadioButtons=2,labelArray2=['Average','Subsection'],sl=2,columnWidth3=[60,90,90],columnAlign3=['left','left','left'])
        mc.separator(w=255,style='single')
        importStr = "import %s as ikSplCreate\nikSplCreate.OF_zchBuildSplineIkFromJoints()"%( __name__ )
        applyButton = mc.button(w=255,l='------------Apply------------',c=importStr)
        mc.separator(w=255,style='single')
        Blog = mc.text('(Author) OF3D-TD| Zhao ChunHai 2012/08/10',en=0)
        mc.separator(w=255,style='single')
        mc.setParent(tabs)
        
        rollIk = mc.frameLayout( collapsable= 0 ,borderStyle ="etchedOut" ,labelVisible =0 ,marginWidth =0 ,marginHeight =0)
        mc.columnLayout(adj=1)
        nameButton = mc.textFieldGrp('rollIk_Name_tfg',l='SplineName: ',columnWidth2=[85,200],cl2=['center','left'])
        mc.separator(w=255,style='single')
        
        disDes = mc.floatSliderGrp('rollIk_disDes_fsg',label='Distance --:',h=25,min=0,max=1,v=0.05,field=True,precision=3,columnWidth3=[90,50,120],adjustableColumn3=3)
        minDisDes = mc.floatSliderGrp('rollIk_minDes_fsg',label='Min Des:',h=25,min=0,max=1,v=0.55,field=True,precision=3,columnWidth3=[90,50,120],adjustableColumn3=3)
        
        njnts = mc.intSliderGrp('rollIk_NumJnts_isg',label='Joint Num:',h=25,min=2,max=99,v=5,field=True,columnWidth3=[90,50,120],adjustableColumn3=3)
        manyCtrls = mc.intSliderGrp('rollIk_NumCtrls_isg',label='Many Ctrls:',h=25,min=2,max=99,v=6,field=True,columnWidth3=[90,50,120],adjustableColumn3=3)
        
        mc.separator(w=255,style='single')
        mc.checkBoxGrp('rollIk_Option_cbg',label="   ",ncb=2,la2=['Stretch','keepVolume'],columnWidth3=[30,120,120],va2=[1,1],vis=0)
        mc.separator(w=255,style='single')
        zchSplineIkType = mc.radioButtonGrp('rollIk_StrType_rbg',label='  Stretch: ',sl=2,en=0,
                                                        numberOfRadioButtons=2,
                                                        labelArray2=['Scale','Translate'],
                                                        columnWidth3=[60,90,90],
                                                        columnAlign3=['left','left','left'])
        mc.separator(w=255,style='single')
        importStr = "import %s as ikSplCreate\nikSplCreate.rollIkBuild()"%( __name__ )
        applyButton = mc.button(w=255,l='------------Apply------------',c=importStr)
        mc.separator(w=255,style='single')
        Blog = mc.text('(Author) OF3D-TD| Zhao ChunHai 2012/08/10',en=0)
        mc.separator(w=255,style='single')
        mc.setParent('..')
        
        mc.tabLayout(tabs,e=True,edit=True,tabLabel=[(byCurve_,"IkSplineFrom>Curve"),(byJoint_,"IkSplineFrom>IkHandle"),(rollIk,"IkSpline>Roll ikSpline")],sti=3)
        #mc.window('char_createSplineIkWin',e=True,w=300,h=169)
        mc.showWindow('char_createSplineIkWin')
        
def OF_zchBuildSplineIk():
        zchSplineIkName = mc.textFieldGrp('OF_zchSplineIkName_tfg',q=True,text=True)
        jointNum = mc.intSliderGrp('OF_zchSplineIkNJnts_isg',q=True,v=True)
        stretch =  mc.checkBoxGrp('OF_zchSplineIkOption_cbg',q=True,v1=True)
        keepVolume = mc.checkBoxGrp('OF_zchSplineIkOption_cbg',q=True,v2=True)
        scTr = mc.radioButtonGrp('OF_zchSplineIkStrType_rbg',q=True,sl=True)
        strType = mc.radioButtonGrp('OF_zchSplineIkKvType_rbg',q=True,sl=True)
        splineAbout=char_stretchSplineIk(zchSplineIkName,jointNum,scTr,keepVolume,strType)
        sys.stderr.write('Reult: ...\n')
        
        
def OF_zchBuildSplineIkFromJoints():
        splineName = mc.textFieldGrp("ikspl_fjSetSplName_tfg",q=True,text=True)
        ikhandle = mc.textFieldButtonGrp("ikspl_fjLoadIkHandle_tfg",q=True,text=True)
        #jointStart = mc.textFieldButtonGrp("ikspl_fjLoadIkHandle_tfg",q=True,text=True)
        #jointEnd = mc.textFieldButtonGrp("ikspl_fjLoadIkHandle_tfg",q=True,text=True)
        jointStart = mc.ikHandle(ikhandle,q=True,sj=True)
        jointEnd = mc.ikHandle(ikhandle,q=True,ee=True)
        cruveShape = mc.ikHandle(ikhandle,q=True,curve=True)
        curveTrans = mc.listRelatives(cruveShape,p=True)[0]
        
        scTr = mc.checkBoxGrp('ikspl_fjIkOption_cbg',q=True,v1=True)
        if scTr!=0:
            scTr = mc.radioButtonGrp('ikspl_fjStrType_rbg',q=True,sl=True)
        
        keepVolume = mc.checkBoxGrp('ikspl_fjIkOption_cbg',q=True,v2=True)
        
        strType = mc.radioButtonGrp('ikspl_fjIkKvType_rbg',q=True,sl=True)
        
        ikSpl.ikSplineFromJoints(splineName,curveTrans,jointStart,jointEnd,scTr,keepVolume,strType)
        sys.stderr.write('Reult: ...\n')
def rollIkBuild():
    splineName = mc.textFieldGrp('rollIk_Name_tfg',q=True,text=True)
    des = mc.floatSliderGrp('rollIk_disDes_fsg',q=True,v=True)
    minDisDes = mc.floatSliderGrp('rollIk_minDes_fsg',q=True,v=True)
    counts = mc.intSliderGrp('rollIk_NumJnts_isg',q=True,v=True)
    manyCtrls = mc.intSliderGrp('rollIk_NumCtrls_isg',q=True,v=True)
    
    stretch =  mc.checkBoxGrp('rollIk_Option_cbg',q=True,v1=True)
    keepVolume = mc.checkBoxGrp('rollIk_Option_cbg',q=True,v2=True)
    scTr = mc.radioButtonGrp('rollIk_StrType_rbg',q=True,sl=True)
    
    ikSpl.rollIkspline(splineName,des,counts,manyCtrls,minDisDes)
    
    
    
    
    
    
    
    
    
